var t = getApp();

Page({
    data: {
        teamId: "",
        data: []
    },
    loadData: function() {
        var a = this;
        t.network.post({
            url: t.url.teamEchelonMemberList,
            params: {
                id: a.data.teamId
            },
            before: function() {
                wx.showNavigationBarLoading();
            },
            success: function(e) {
                1 == e.data.result ? (console.log(e.data.data.members), a.setData({
                    data: e.data.data.members
                })) : t.util.showToast(e.data.message);
            },
            fail: function() {
                t.util.showToast("加载失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
            }
        });
    },
    saveData: function(a) {
        t.network.post({
            url: t.url.teamMemberSort,
            params: {
                data: a
            },
            before: function() {
                wx.showNavigationBarLoading(), wx.showLoading({
                    title: "正在保存"
                });
            },
            success: function(a) {
                wx.hideLoading(), t.util.showToast(a.data.message), 1 == a.data.result && setTimeout(function() {
                    wx.navigateBack({});
                }, 1200);
            },
            fail: function() {
                wx.hideLoading(), t.util.showToast("保存失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    delectMember: function(a) {
        var e = this;
        t.network.post({
            url: t.url.deleteTeamMember,
            params: {
                id: e.data.teamId,
                deleteAccessToken: a
            },
            before: function() {
                wx.showLoading({
                    title: "正在删除"
                });
            },
            success: function(a) {
                wx.hideLoading(), t.util.showToast(a.data.message), 1 == a.data.result && e.loadData();
            },
            fail: function() {
                wx.hideLoading(), t.util.showToast("删除失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    clickDel: function(t) {
        var a = t.currentTarget.dataset.token, e = t.currentTarget.dataset.name, o = this;
        wx.showModal({
            title: "提示",
            content: "确定要" + e + "把移出团队吗？",
            success: function(t) {
                t.confirm && o.delectMember(a);
            }
        });
    },
    clickDown: function(t) {
        var a = this.data.data, e = t.currentTarget.dataset.teamindex, o = t.currentTarget.dataset.index, s = a[e].userList;
        if (console.log(o), console.log(s), console.log("teamIndex" + e), o == s.length - 1) {
            n = a[e].userList.pop();
            a[e + 1].userList.unshift(n), 0 == e && 0 == a[0].userList.length && "" == a[0].echelonName && a.shift(), 
            console.log("跨组", a), this.setData({
                data: a
            });
        } else {
            var n = a[e].userList.splice(o, 1);
            a[e].userList.splice(o + 1, 0, n[0]), console.log("同组", a), this.setData({
                data: a
            });
        }
    },
    clickUp: function(t) {
        var a = this.data.data, e = t.currentTarget.dataset.teamindex, o = t.currentTarget.dataset.index;
        a[e].userList;
        if (0 == o) {
            if (1 == e && "" == a[0].echelonName) return;
            s = a[e].userList.shift();
            a[e - 1].userList.push(s), console.log("跨组", a), this.setData({
                data: a
            });
        } else {
            var s = a[e].userList.splice(o, 1);
            a[e].userList.splice(o - 1, 0, s[0]), console.log("同组", a), this.setData({
                data: a
            });
        }
    },
    clickConfirm: function(t) {
        for (var a = 0, e = [], o = 0; o < this.data.data.length; o++) for (var s = this.data.data[o].userList, n = 0; n < s.length; n++) {
            var i = {
                echelonId: this.data.data[o].echelonId,
                accessToken: s[n].accessToken,
                level: a
            };
            a += 1, e = e.concat(i);
        }
        var c = JSON.stringify(e);
        console.log(c), this.saveData(c);
    },
    onLoad: function(t) {
        this.setData({
            teamId: t.teamId
        }), this.loadData();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});